import axios from './http'
import store from './store'
import * as types from './types'
import {Message} from 'element-ui'
import {ServerError} from './error'

export class Urls {
  // captcha的地址不能写死 http 或者 https
  static captcha = '//captcha.luosimao.com/static/dist/api.js'
  // todo 上线后修改地址为 http://cdn.nicesite.vip
  static img = 'http://p3z15mvur.bkt.clouddn.com/'
}

export class AccountService {
  static login (auth) {
    return axios.post('login', auth)
      .then(res => {
        if (res.data) {
          loginCommit(res.data)
        }
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static register (auth) {
    return axios.post('register', auth)
      .then(res => {
        if (res.data) {
          loginCommit(res.data)
          UserService.me()
        }
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static logout () {
    return axios.get('token/logout')
      .then(res => {
        store.commit(types.LOGOUT)
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static resetPass (auth) {
    return axios.post('reset/pass', auth)
      .then(res => {
        console.log(res)
        if (res.data === 'success') {
          store.commit(types.LOGOUT)
        }
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static refresh () {
    if (store.state.exp - new Date().getTime() > 3 * 3600 * 1000) {
      return
    }
    return axios.get('token/refresh')
      .then(res => {
        if (res.data) {
          loginCommit(res.data)
        }
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }
}

function loginCommit (auth) {
  store.commit(types.LOGIN, auth.jwt)
  store.commit(types.EXP, auth.exp)
}

export class UserService {
  static me () {
    return axios.get('user/me')
      .then(res => {
        if (res.data) {
          let user = res.data.user
          user.role = res.data.role
          user.username = user.user_name
          // let r = res.data.role
          // let user = {
          //   uid: u.id,
          //   username: u.user_name,
          //   email: u.email,
          //   phone: u.phone,
          //   role: r
          // }
          store.commit(types.USER, user)
        }
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }

  static page (num, size) {
    return axios.post('user/page', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static online () {
    return !!store.state.jwt
  }
}

export class SecurityService {
  static qiniuToken () {
    return axios.get('token/qiniu')
      .then(res => {
        return Promise.resolve(res.data)
      }).catch(
        error => {
          Message.error(ServerError.Translate(error))
        }
      )
  }
}

export class NoticeService {
  static one (id) {
    return axios.post('notice/one', {
      id: parseInt(id)
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static page (num, size) {
    return axios.post('notice/page', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true,
      order: 'created_at'
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static pageAll (num, size) {
    return axios.post('notice/page/all', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static create (form) {
    return axios.post('notice/create', form).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static toggle (id) {
    return axios.post('notice/toggle', {
      id: parseInt(id)
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }
}

export class PostService {
  static one (id, module) {
    return axios.post('post/one', {
      id: parseInt(id),
      module: module
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static page (num, size, module) {
    return axios.post('post/page', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true,
      order: 'created_at',
      module: module
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static pageMe (num, size) {
    return axios.post('post/page/me', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static pageAll (num, size) {
    return axios.post('post/page/all', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static create (form) {
    form.author = store.state.user.username
    return axios.post('post/create', form).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static countMe () {
    return axios.get('post/count/me').then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static toggle (id) {
    return axios.post('post/toggle', {
      id: parseInt(id)
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }
}

export class MsgService {
  static one (id, module) {
    return axios.post('msg/one', {
      id: parseInt(id),
      module: module
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static page (num, size) {
    return axios.post('msg/page', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true,
      order: 'created_at'
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static pageMe (num, size) {
    return axios.post('msg/page/me', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static pageAll (num, size) {
    return axios.post('msg/page/all', {
      page_num: parseInt(num),
      page_size: parseInt(size),
      desc: true
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static create (pub, title, content) {
    return axios.post('msg/create', {
      public: pub,
      title: title,
      content: content,
      author: store.state.user.username
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static countMe () {
    return axios.get('msg/count/me').then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }

  static toggle (id) {
    return axios.post('post/toggle', {
      id: parseInt(id)
    }).then(res => {
      return Promise.resolve(res.data)
    }).catch(
      error => {
        Message.error(ServerError.Translate(error))
      }
    )
  }
}

export class LocalService {
  static SaveTmpMsg (title, content) {
    localStorage.msgTitle = title
    localStorage.msgContent = content
  }

  static GetTmpMsg () {
    return {
      title: localStorage.msgTitle,
      content: localStorage.msgContent
    }
  }

  static SaveTmpPost (title, content, module) {
    localStorage.postTitle = title
    localStorage.postContent = content
    localStorage.postModule = module
  }

  static GetTmpPost () {
    return {
      title: localStorage.postTitle,
      content: localStorage.postContent,
      module: localStorage.postModule
    }
  }

  static SaveIndexPath (path) {
    localStorage.indexPath = path
  }

  static GetIndexPath () {
    return localStorage.indexPath
  }

  static SaveUserPath (path) {
    localStorage.userPath = path
  }

  static GetUserPath () {
    return localStorage.userPath
  }
}
